const { findOne,create } = require('../models/user')

function checkUsername(username) {
  let pattern = /^[0-9a-zA-Z-_]{4,16}$/
  return pattern.test(username)
}

async function checkUserRename(username) {
  return await findOne({ query: { username } })
}

function checkRePassword(password,rePassword){
  return password === rePassword
}

function checkPassword(password){
  let pattern = /^[0-9a-zA-Z]{4,16}$/
  return pattern.test(password)
}

function checkAge(age){
  return !(Number(age) > 120 || Number(age) < 0)
}

async function saveUser(data){
  await create(data)
}
async function userLogin({username,password}){
 
  return await findOne({query:{ username,password}})
}
async function getUserInfo(id){

  return await findOne({ query: { _id: id }}) 
}

module.exports = {
  checkUsername,
  checkUserRename,
  checkRePassword,
  checkPassword,
  checkAge,
  saveUser,
  getUserInfo,
  userLogin
}